/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: Anonymous
 * License Type: Purchased
 */
package com.estate.datalogic.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import com.estate.datalogic.*;

/**
 * ORM-Persistable Class
 */
public class ClientDAOImpl implements com.estate.datalogic.dao.ClientDAO {
	private static final org.apache.log4j.Logger _logger = org.apache.log4j.Logger.getLogger(ClientDAOImpl.class);
	public static Client[] listByuid(String uid) throws PersistentException {
		try {
			PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
			return listByuid(session, uid);
		}
		catch (Exception e) {
			_logger.error("listByuid(String uid)", e);
			throw new PersistentException(e);
		}
	}
	
	public static Client[] listByuid(PersistentSession session, String uid) throws PersistentException {
		try {
			Query query = session.createQuery("From com.estate.datalogic.Client As Client Where Client.uid = ?");
			query.setParameter(0, uid);
			List list = query.list();
			return (Client[]) list.toArray(new Client[list.size()]);
		}
		catch (Exception e) {
			_logger.error("listByuid(PersistentSession session, String uid)", e);
			throw new PersistentException(e);
		}
	}
	
	public static Client loadByuid(String uid) throws PersistentException {
		try {
			PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
			return loadByuid(session, uid);
		}
		catch (Exception e) {
			_logger.error("loadByuid(String uid)", e);
			throw new PersistentException(e);
		}
	}
	
	public static Client loadByuid(PersistentSession session, String uid) throws PersistentException {
		Client[] clients = listByuid(session, uid);
		if (clients != null && clients.length > 0)
			return clients[0];
		else
			return null;
	}
	
	public Client loadClientByORMID(int id) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return loadClientByORMID(session, id);
	}
	
	public Client loadClientByORMID(PersistentSession session, int id) throws PersistentException {
		return (Client) session.load(com.estate.datalogic.Client.class, new Integer(id));
	}
	
	public Client[] listClientByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return listClientByQuery(session, condition, orderBy);
	}
	
	public Client[] listClientByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From com.estate.datalogic.Client as Client");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			List list = query.list();
			return (Client[]) list.toArray(new Client[list.size()]);
		}
		catch (Exception e) {
			_logger.error("listClientByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Client loadClientByQuery(String condition, String orderBy) throws PersistentException {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return loadClientByQuery(session, condition, orderBy);
	}
	
	public Client loadClientByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Client[] clients = listClientByQuery(session, condition, orderBy);
		if (clients != null && clients.length > 0)
			return clients[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateClientByQuery(String condition, String orderBy) throws PersistentException  {
		PersistentSession session = com.estate.datalogic.EstatePersistentManager.instance().getSession();
		return iterateClientByQuery(session, condition, orderBy);
	}
	
	public static java.util.Iterator iterateClientByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From com.estate.datalogic.Client as Client");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		Query query = session.createQuery(sb.toString());
		try {
			return query.iterate();
		}
		catch (Exception e) {
			_logger.error("iterateClientByQuery(PersistentSession session, String condition, String orderBy)", e);
			throw new PersistentException(e);
		}
	}
	
	public Client createClient() {
		return new com.estate.datalogic.Client();
	}
	
	public boolean save(com.estate.datalogic.Client client)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().saveObject(client);
			return true;
		}
		catch (Exception e) {
			_logger.error("save(com.estate.datalogic.Client client)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(com.estate.datalogic.Client client)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().deleteObject(client);
			return true;
		}
		catch (Exception e) {
			_logger.error("delete(com.estate.datalogic.Client client)", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(com.estate.datalogic.Client client)throws PersistentException {
		try {
			com.estate.datalogic.Advertisement[] lAdvertisements = client.advertisement.toArray();
			for(int i = 0; i < lAdvertisements.length; i++) {
				lAdvertisements[i].setClient(null);
			}
			return delete(client);
		}
		catch(Exception e) {
			_logger.error("deleteAndDissociate()", e);
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(com.estate.datalogic.Client client)throws PersistentException {
		try {
			com.estate.datalogic.EstatePersistentManager.instance().getSession().refresh(client);
			return true;
		}
		catch (Exception e) {
			_logger.error("refresh(com.estate.datalogic.Client client)", e);
			throw new PersistentException(e);
		}
	}
	
}
